using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using MM.Entities;
namespace MM.Business
{
    public class clsTHAM_SO
    {
        public clsTHAM_SO(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private THAM_SO GetDataRow(DataRow row)
        {
            THAM_SO objTHAM_SO = new THAM_SO();
            try
            {
                if (row["MaThamSo"] != DBNull.Value)
                    objTHAM_SO.MaThamSo = row["MaThamSo"].ToString();
            }
            catch { }
         
            try
            {
                if (row["TenThamSo"] != DBNull.Value)
                    objTHAM_SO.TenThamSo = (row["TenThamSo"].ToString());
            }
            catch { }
            try
            {
                if (row["TenForm"] != DBNull.Value)
                    objTHAM_SO.TenForm = row["TenForm"].ToString();
            }
            catch { }
           
            try
            {
                if (row["GiaTri"] != DBNull.Value)
                    objTHAM_SO.GiaTri = row["GiaTri"].ToString();
            }
            catch { }

            try
            {
                if (row["DaXoa"] != DBNull.Value)
                    objTHAM_SO.DaXoa = int.Parse(row["DaXoa"].ToString());
            }
            catch { }
           
            return objTHAM_SO;
        }
        private List<THAM_SO> GetTHAM_SO(DataTable dt)
        {
            List<THAM_SO> lst = new List<THAM_SO>();
            THAM_SO obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private Method

        public DataTable usp_SelectTHAM_SO(string MaThamSo)
        {
            string spName = "usp_SelectTHAM_SO";
            DataTable dt = new DataTable();

            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaThamSo", MaThamSo);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return dt;
        }

        public THAM_SO SelectTHAM_SO(string MaThamSo)
        {
            List<THAM_SO> lst = new List<THAM_SO>();
            DataTable dt = new DataTable();
            dt = this.usp_SelectTHAM_SO(MaThamSo);
            lst = this.GetTHAM_SO(dt);

            if (lst.Count > 0)
                return lst[0];
            return null;
        }

        public List<THAM_SO> usp_SelectTHAM_SO_All()
        {
            string spName = "select * from tham_so order by tenform";
            DataTable dt = new DataTable();
            List<THAM_SO> lst = new List<THAM_SO>();
            try
            {
                SqlHelper.OpenConnection();
              
                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName,CommandType.Text);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                }
                lst = this.GetTHAM_SO(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public int usp_InsertUpdateTHAM_SO(THAM_SO obj)
        {
            int iResult = 0;
            string spName = "usp_InsertUpdateTHAM_SO";

            try
            {
                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaThamSo", obj.MaThamSo);
                par[1] = new SqlParameter("@TenForm", obj.TenForm);
                par[2] = new SqlParameter("@TenThamSo", obj.TenThamSo);
                par[3] = new SqlParameter("@GiaTri", obj.GiaTri);              
                
                if (SqlHelper.IsHaveTran())
                    iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                else
                    iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

            }
            catch (Exception ex)
            {
            }
            return iResult;
        }
    }
}
